package com.kejiguan.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kejiguan.ui.R;
import com.kejiguan.ui.model.Shedule;

import java.util.List;

/**
 * Created by apple on 16/5/13.
 */
public class SheduleAdapter extends RecyclerView.Adapter<SheduleAdapterVH> {


    private LayoutInflater inflater;
    private List<Shedule> shedules;
    private View.OnLongClickListener longClickListener;
    private View.OnClickListener onClickListener;

    public SheduleAdapter(Context context, List<Shedule> shedules) {
        inflater = LayoutInflater.from(context);
        this.shedules = shedules;
    }


    @Override
    public SheduleAdapterVH onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.fragment_shedule_list_item_layout, null);
        return new SheduleAdapterVH(itemView);
    }

    @Override
    public void onBindViewHolder(SheduleAdapterVH holder, int position) {

        Shedule shedule = shedules.get(position);
        holder.sheduleName.setText(shedule.getScheduleName());
        holder.sheduleObject.setText("TALL".equals(shedule.getScheduleObject()) ? "所有" : shedule.getScheduleObject());
        holder.sheduleAction.setText("K".equals(shedule.getScheduleAction()) ? "开" : "关");
        holder.sheduleTime.setText(shedule.getScheduleTime());
        String type = shedule.getScheduleType();
        switch (type) {
            case "1":
                holder.sheduleType.setText("仅此一次");
                break;
            case "2":
                holder.sheduleType.setText("周一到周五");
                break;
            case "4":
                holder.sheduleType.setText("每天");
                break;
            default:
                String[] types = type.split(",");
                if (null != types && types.length == 2) {
                    switch (types[1]) {

                        case "MON":
                            holder.sheduleType.setText("星期一");
                            break;
                        case "TUE":
                            holder.sheduleType.setText("星期二");
                            break;
                        case "WED":
                            holder.sheduleType.setText("星期三");
                            break;
                        case "THU":
                            holder.sheduleType.setText("星期四");
                            break;
                        case "FRI":
                            holder.sheduleType.setText("星期五");
                            break;
                        case "SAT":
                            holder.sheduleType.setText("星期六");
                            break;
                        case "SUN":
                            holder.sheduleType.setText("星期日");
                            break;
                        default:
                            holder.sheduleType.setText("未知");
                            break;
                    }

                } else {
                    holder.sheduleType.setText("未知");
                }
                break;
        }

        if (null!=longClickListener)
        {
            holder.itemView.setTag(R.id.recyclerview_item_position,position);
            holder.itemView.setOnLongClickListener(longClickListener);

        }

        if (null!=onClickListener)
        {
            holder.itemView.setTag(R.id.recyclerview_item_position,position);
            holder.itemView.setOnClickListener(onClickListener);

        }
    }

    @Override
    public int getItemCount() {
        if (null == shedules) {
            return 0;
        } else {
            return shedules.size();
        }
    }


    public Shedule getItemObject(int position)
    {
        return shedules.get(position);
    }

    public void setShedules(List<Shedule> shedules) {
        this.shedules = shedules;
        notifyDataSetChanged();
    }

    public void setLongClickListener(View.OnLongClickListener longClickListener) {
        this.longClickListener = longClickListener;
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }
}
